
package com.wyzz.hr.bo.impl;

import java.util.Date;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.wyzz.hr.bo.IPositionBaseHistBo;
import com.wyzz.hr.dao.IPositionBaseHistDao;
import com.wyzz.hr.pojo.Department;
import com.wyzz.hr.pojo.PositionBase;
import com.wyzz.hr.pojo.PositionBaseHist;


public class PositionBaseHistBoImpl implements IPositionBaseHistBo {

	public PositionBaseHistBoImpl() {
	}

	public IPositionBaseHistDao getPositionBaseHistDao() {
		return positionBaseHistDao;
	}

	public void setPositionBaseHistDao(IPositionBaseHistDao positionBaseHistDao) {
		this.positionBaseHistDao = positionBaseHistDao;
	}

	public List getRequirePbhList(Date queryDate, String deptId) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.PositionBaseHist.class);
		dc.add(Restrictions.eq(PositionBaseHist.PROP_PBHI_PB_STATUS, Integer
				.valueOf(1)));
		dc.add(Restrictions.eq(PositionBaseHist.PROP_PBHI_DEPT_ID,
				new Department(deptId)));
		dc.add(Restrictions
				.le(PositionBaseHist.PROP_PBHI_VALID_FROM, queryDate));
		dc.add(Restrictions.or(Restrictions.ge(
				PositionBaseHist.PROP_PBHI_VALID_TO, queryDate), Restrictions
				.isNull(PositionBaseHist.PROP_PBHI_VALID_TO)));
		return positionBaseHistDao.findByCriteria(dc);
	}

	public String getDeptChargePb(Date queryDate, String deptId) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.PositionBaseHist.class);
		dc.add(Restrictions.eq(PositionBaseHist.PROP_PBHI_IN_CHARGE, Integer
				.valueOf(1)));
		dc.add(Restrictions.eq(PositionBaseHist.PROP_PBHI_PB_STATUS, Integer
				.valueOf(1)));
		dc.add(Restrictions.eq(PositionBaseHist.PROP_PBHI_DEPT_ID,
				new Department(deptId)));
		dc.add(Restrictions
				.le(PositionBaseHist.PROP_PBHI_VALID_FROM, queryDate));
		dc.add(Restrictions.or(Restrictions.ge(
				PositionBaseHist.PROP_PBHI_VALID_TO, queryDate), Restrictions
				.isNull(PositionBaseHist.PROP_PBHI_VALID_TO)));
		List pbList = positionBaseHistDao.findByCriteria(dc);
		if (pbList != null && pbList.size() == 1)
			return ((PositionBaseHist) pbList.get(0)).getPbhiPbId().getId();
		else
			return null;
	}

	public PositionBaseHist getLatestPBHist(PositionBase pb) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.PositionBaseHist.class);
		dc.add(Restrictions.eq(PositionBaseHist.PROP_PBHI_PB_ID, pb));
		dc.add(Restrictions.isNull(PositionBaseHist.PROP_PBHI_VALID_TO));
		List pbHistList = positionBaseHistDao.findByCriteria(dc);
		if (pbHistList.size() > 0)
			return (PositionBaseHist) pbHistList.get(0);
		else
			return null;
	}

	public List getPbHistsOfPb(String pbId) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.PositionBaseHist.class);
		dc.add(Restrictions.eq(PositionBaseHist.PROP_PBHI_PB_ID,
				new PositionBase(pbId)));
		return positionBaseHistDao.findByCriteria(dc);
	}

	public List exeHqlList(String hql) {
		return positionBaseHistDao.exeHqlList(hql);
	}

	private IPositionBaseHistDao positionBaseHistDao;
}
